﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using StudentWeb.BLL;
using Newtonsoft.Json.Linq;
using StudentWeb.Common;
using System.Web.Http;
using StudentWeb.DAL;
using StudentWeb.Model;

namespace StudentWeb.Api.Controllers
{
    public class CatController : ApiController
    {
        CatBO _catBo = new CatBO();
        #region 猫咪信息 单条记录
        /// <summary>
        /// 猫咪信息 单条记录
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpGet]
        ///[ValidToken]
        public IHttpActionResult GetCatInfo(int catID)
        {
            try
            {
                var result = _catBo.GetCatInfo(catID);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 猫猫信息 添加
        /// <summary>
        /// 猫猫信息 添加
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult InsertCatInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                var result = _catBo.InsertCatInfo(model);
                return result ? Json(new Result()) : Json(new Result(ResultCode.Fail, "保存"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 猫猫信息 更新
        /// <summary>
        /// 猫猫信息 添加
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult UpdateCatInfo([FromBody]Object obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                var result = _catBo.UpdateCatInfo(model);
                return result ? Json(new Result()) : Json(new Result(ResultCode.Fail, "保存失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 猫猫信息 保存单条记录
        /// <summary>
        /// 猫猫信息 保存单条记录
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult SaveCatInfo([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                var result = _catBo.SaveCatInfo(model);
                return result ? Json(new Result()) : Json(new Result(ResultCode.Fail, "保存失败"));
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }

        #endregion

        #region 猫猫记录 删除单条
        [HttpPost]
        public IHttpActionResult DeleteCatInfo([FromBody] JObject obj)
        {
            if (obj == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                var result = _catBo.DeleteCatInfo(obj["CatDataIDLLJJ"].ToObject<int>());
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 查询猫猫信息列表
        public IHttpActionResult GetCatList()
        {
            try
            {
                var result = _catBo.GetCatList();
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        #endregion

        #region 猫猫信息 分页列表
        /// <summary>
        /// 猫猫信息 分页列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IHttpActionResult GetCatList([FromBody]JObject obj)
        {
            dynamic model = obj;
            if (model == null) return Json(new Result(ResultCode.Fail, "缺少参数"));
            try
            {
                int pageIndex = model.page_index ?? 0;
                int pageSize = model.page_size ?? 0;
                if (pageIndex < 1 || pageSize < 1) return Json(new Result(ResultCode.Fail, "参数错误"));
                var wc = _GetCatListWhereC(model.condidata);
                var result = _catBo.GetCatList(pageIndex, pageSize, wc);
                return Json(result);
            }
            catch (Exception ex)
            {
                return Json(new Result(ResultCode.Fail, ex.Message));
            }
        }
        private string _GetCatListWhereC(dynamic qc)
        {
            var wc = string.Empty;
            if (qc == null) return wc;
            string tmp = "";
            tmp = ConvertHelper.DynamicToString(qc.bookName);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" and CatNameLLJJ like '%{tmp}%'"; }
            tmp = ConvertHelper.DynamicToString(qc.bookNo);
            if (!string.IsNullOrEmpty(tmp)) { wc += $" and CatSexLLJJ like '%{tmp}%'"; }
            return wc;
        }
        #endregion
    }
}